<?php
/**
 * Created by PhpStorm.
 * User: Guangpeng Chen
 * Date: 15-8-5
 * Time: 下午4:36
 *      URL:http://www.12301.cc/api/terminal_merge.php
 *      action:
 *            GET:action=TERMINAL_DIVIDE,
 *            POST:action=TERMINAL_DELETE,id=记录ID
 *            GET:action=TERMINAL_LIST,salerid=uu_land表salerid
 */
include('/var/www/html/new/com.inc.php');
include('/var/www/html/new/d/common/func.inc.php');
$action = $_REQUEST['action'];
write_logs(json_encode($_GET), '../logs/debug.txt');

switch($action) {
    //分终端
    case 'TERMINAL_DIVIDE':
        ksort($_GET);
        $terminals = array();
        $chk_auth   = $_GET['auth'];
        unset($_GET['auth']);
        $auth       = md5(implode(',', $_GET));
        if ($auth!=$chk_auth) {
            exit('{"status":"fail","msg":"校验失败"}');
        }
        $main_id     = intval($_GET['main_id']);
        $merge_num   = intval($_GET['merge_num']);
        $mid         = intval($_GET['mid']);
        if (!$main_id || !$merge_num) {
            exit('{"status":"fail","msg":"参数错误"}');
        }
        $list           = array();
        $insert_values  = '';
        $sql = "SELECT title,terminal,salerid,p_type FROM uu_land WHERE id=$main_id LIMIT 1";
        $GLOBALS['le']->query($sql);
        $list = $GLOBALS['le']->fetch_assoc();
        if ($list['p_type']=='F') {
            exit('{"status":"fail","msg":"套票无法合并终端"}');
        }
        $terminals[0] = array('terminal_id'=>$GLOBALS['le']->f('terminal'), 'is_main'=>1);
        //判断是否被合并的记录已存在
        $sql_main_merged = "SELECT nowSalerID FROM pft_terminal_share WHERE preSalerID={$list['salerid']} LIMIT 1";
        $GLOBALS['le']->query($sql_main_merged);
        $list_main_merged = $GLOBALS['le']->fetch_assoc();
        if (!$list_main_merged) {
            //统一处理新的salerid
            $memParams = array(
                'dname'    => $list['title'].'-终端合并',
                'dtype'    => 3,//终端合并后的直接供应方
                'password' => 'b81b84c7961ac41a9e619458511a98d9',//md5(md5('uu654321'))
            );
            //生成直接供应商
            include '/var/www/html/new/d/class/MemberAccount.class.php';
            $mem = new \pft\Member\MemberAccount($GLOBALS['le']);
            $reg_res = $mem->register($memParams, array());
//            print_r($reg_res);
            if($reg_res['status']=='ok') {
                $res_account = explode('|', $reg_res['body']);
                $nowSalerID  = $res_account[1];

                $insert_values .= <<<SQL
        ({$list['terminal']},{$list['terminal']},{$list['salerid']},{$nowSalerID}, 1, now(),'',{$mid}),
SQL;
            }
        }
        else {
            $nowSalerID  = $list_main_merged['nowSalerID'];
        }
        //获取终端号
        $max_terminal_log = '/var/www/html/new/d/common/max_terminal_id.txt';
        $max_terminal = (int)HandleLastData($max_terminal_log);

        for ($i=0;$i<$merge_num;$i++) {
            $max_terminal = $max_terminal + 1;
            $terminals[] = array('terminal_id'=>$max_terminal, 'is_main'=>1);
            $insert_values .= <<<SQL
        ($max_terminal,{$list['terminal']},0,{$nowSalerID}, 0,now(),'',{$mid}),
SQL;
        }
        HandleLastData($max_terminal_log, 1, $max_terminal);
        if (empty($insert_values)) {
            exit('{"status":"fail","msg":"数据不存在"}');
        }
        $insert_values = rtrim($insert_values, ',');
        $sql_insert = <<<SQL
INSERT INTO pft_terminal_share(preTerminal,nowTerminal,preSalerID,nowSalerID,
dstatus,addtime,verify_time,privID) VALUES $insert_values
SQL;
        write_logs($sql_insert, '../logs/debug.txt');
        $GLOBALS['le']->query($sql_insert);
        echo json_encode(array('status'=>'ok', 'terminals'=>$terminals));
        break;
    case 'TERMINAL_DELETE':
        if ($_SESSION['memberID']) {
            $id = intval($_POST['id']);
            if ($id) {
                $sql = "UPDATE pft_terminal_share SET dstatus=2 WHERE id=$id LIMIT 1";
                $GLOBALS['le']->query($sql);
                write_logs("{$_SESSION['memberID']}:$sql", '../logs/terminal_check/t.log');
                echo '{"code":200}';
            }
        } else {
            echo '{"code":102}';
        }
        break;
    case 'TERMINAL_LIST':
        $salerid = safetxt($_GET['salerid']);
        if (!is_numeric($salerid)) {
            echo '{"code":107}';
            exit;
        }
        $sql = "SELECT id,nowTerminal,dstatus FROM pft_terminal_share WHERE preSalerID='{$salerid}' AND dstatus<2";
        $GLOBALS['le']->query($sql);
        while($row=$GLOBALS['le']->fetch_assoc()) {
            $output[] = $row;
        }
        echo json_encode(array('code'=>200,'list'=>$output));
        break;
}
